package com.system.connect.springboot_flowers.service;

import com.system.connect.springboot_flowers.entity.Subscribe;
import com.system.connect.springboot_flowers.mapper.SubscribeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class SubscribeService {
    @Autowired
    private SubscribeMapper subscribeMapper;

    @Transactional
    public boolean subscribe(String email) {
        // 检查是否已经订阅
        Subscribe existing = subscribeMapper.findByEmail(email);
        if (existing != null) {
            return false;
        }

        // 创建新的订阅
        Subscribe subscribe = new Subscribe();
        subscribe.setEmail(email);
        subscribe.setCreateTime(LocalDateTime.now());
        subscribe.setStatus(0); // 未验证状态

        return subscribeMapper.insert(subscribe) > 0;
    }
}